package com.cction.comm;

import com.cction.entry.CctionBean;
import com.cction.entry.DetailBean;

import java.util.ArrayList;

public class CctionPresenterImpl implements CctionPresenter {
    /**
     * 复利
     * 公式：本金x(1+利率)^次数
     *
     * @param principal 本金
     * @param interest  利率
     * @param date      次数
     * @return 复利结果
     */
    @Override
    public String ctionCompound(String principal, String interest, String date) {
        double d_principal = Utils.stringToDouble(principal);
        double d_interest = Utils.stringToDouble(interest) / 100;
        double d_date = Utils.stringToDouble(date);

        double result = d_principal * Math.pow(1 + d_interest, d_date);

        return Utils.keepPointString(result);
    }

    /**
     * 本金
     * 公式：最终金额/(1+利率)^次数
     *
     * @param result   最终值
     * @param interest 利率
     * @param date     次数
     * @return 每次投入
     */
    @Override
    public String ctionPrincipal(String result, String interest, String date) {
        double d_result = Utils.stringToDouble(result);
        double d_interest = Utils.stringToDouble(interest) / 100;
        double d_date = Utils.stringToDouble(date);

        double principal = d_result / Math.pow(1 + d_interest, d_date);

        return Utils.keepPointString(principal);
    }

    /**
     * 定投
     * 公式：定投金额*(1+收益率)*[-1+(1+收益率)^次数]/收益率
     *
     * @param principal 每次投入
     * @param interest  收益率
     * @param date      次数
     * @return 定投结果
     */
    @Override
    public String ctionSurely(String principal, String interest, String date) {
        double d_principal = Utils.stringToDouble(principal);
        double d_interest = Utils.stringToDouble(interest) / 100;
        double d_date = Utils.stringToDouble(date);

        double result = d_principal * (1 + d_interest) * (-1 + Math.pow(1 + d_interest, d_date)) / d_interest;
        return Utils.keepPointString(result);
    }

    /**
     * 定投
     * 公式：最终金额/(1+收益率)*[-1+(1+收益率)^次数]/收益率
     *
     * @param result   每次投入
     * @param interest 收益率
     * @param date     次数
     * @return 定投结果
     */
    @Override
    public String ctionSurelyPrincipal(String result, String interest, String date) {
        double d_result = Utils.stringToDouble(result);
        double d_interest = Utils.stringToDouble(interest) / 100;
        double d_date = Utils.stringToDouble(date);

        double principal = d_result / ((1 + d_interest) * (-1 + Math.pow(1 + d_interest, d_date)) / d_interest);
        return Utils.keepPointString(principal);
    }


    /**
     * 明细
     */
    public ArrayList<DetailBean> detail(CctionBean cctionBean) {
        ArrayList<DetailBean> arrayList = new ArrayList<>();
        String interest = cctionBean.interest;
        int position = Utils.stringToInt(cctionBean.date);
        for (int i = 1; i <= position; i++) {
            DetailBean detailBean = new DetailBean();
            detailBean.postion = i;
            String result = "";
            switch (cctionBean.type) {
                case 1:
                case 2:
                    result = ctionCompound(cctionBean.principal, interest, i + "");
                    detailBean.interest_total = Utils.keepPointDouble(Utils.stringToDouble(result) - Utils.stringToDouble(cctionBean.principal));
                    detailBean.interest = Utils.keepPointDouble(detailBean.interest_total / Utils.stringToDouble(cctionBean.principal) * 100);
                    break;
                case 3:
                    result = ctionSurely(cctionBean.principal, interest, i + "");
                    detailBean.interest_total = Utils.keepPointDouble(Utils.stringToDouble(result) - Utils.stringToDouble(cctionBean.principal) * i);
                    double currentPrincipal1 = Utils.stringToDouble(cctionBean.principal) * (i + 1);
                    detailBean.interest = Utils.keepPointDouble(detailBean.interest_total / currentPrincipal1 * 100);
                    break;
                case 4:
                    result = ctionSurely(cctionBean.result, interest, i + "");
                    detailBean.interest_total = Utils.keepPointDouble(Utils.stringToDouble(result) - Utils.stringToDouble(cctionBean.result) * i);
                    double currentPrincipal2 = Utils.stringToDouble(cctionBean.result) * (i + 1);
                    detailBean.interest = Utils.keepPointDouble(detailBean.interest_total / currentPrincipal2 * 100);
                    break;
            }
            detailBean.total = result;
            arrayList.add(detailBean);
        }
        return arrayList;
    }
}
